/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sim;
import java.io.*;


/**
 *
 * @author Vu Duc Tiep
 */
public class GnuPlotparser {
static public String getContents(File aFile) {
    //...checks on aFile are elided
    StringBuilder contents = new StringBuilder();
    String temp=new String();

    try {
      //use buffering, reading one line at a time
      //FileReader always assumes default encoding is OK!
      BufferedReader input =  new BufferedReader(new FileReader(aFile));
      try {
        String line = null; //not declared within while loop
        /*
        * readLine is a bit quirky :
        * it returns the content of a line MINUS the newline.
        * it returns null only for the END of the stream.
        * it returns an empty String if two newlines appear in a row.
        */ 
        
        while (( line = input.readLine()) != null){          
            String[]coordinate=line.split(" ");
            
            temp="(@#`"+coordinate[0]+"-"+coordinate[1]+"'.Nz="+coordinate[2]+"),";
            contents.append(temp);
            contents.append(System.getProperty("line.separator"));
        }        
      }
      finally {
        input.close();
      }
    }
    catch (IOException ex){
      ex.printStackTrace();
    }

    return contents.toString();
  }

  /**
  * Change the contents of text file in its entirety, overwriting any
  * existing text.
  *
  * This style of implementation throws all exceptions to the caller.
  *
  * @param aFile is an existing file which can be written to.
  * @throws IllegalArgumentException if param does not comply.
  * @throws FileNotFoundException if the file does not exist.
  * @throws IOException if problem encountered during write.
  */
  static public void setContents(File aFile, String aContents)
                                 throws FileNotFoundException, IOException {
    if (aFile == null) {
      throw new IllegalArgumentException("File should not be null.");
    }
    if (!aFile.exists()) {
        aFile.createNewFile();
        //throw new FileNotFoundException ("File does not exist: " + aFile);
    }
    if (!aFile.isFile()) {
      throw new IllegalArgumentException("Should not be a directory: " + aFile);
    }
    if (!aFile.canWrite()) {
      throw new IllegalArgumentException("File cannot be written: " + aFile);
    }

    //use buffering
    Writer output = new BufferedWriter(new FileWriter(aFile));
    try {
      //FileWriter always assumes default encoding is OK!
      output.write( aContents );
    }
    finally {
      output.close();
    }
  }

  /** Simple test harness.   */
  public static void main (String... aArguments) throws IOException {
    File testFile = new File("D:\\tes_data.dat");
    String temp=getContents(testFile);
    System.out.println("Original file contents: " + getContents(testFile));
    File waveFile= new File("D:\\waveCode.txt");
    setContents(waveFile,temp );
    //System.out.println("New file contents: " + getContents(testFile));
  }

}
